package com.niit.agricstore.controller;

import com.github.pagehelper.PageInfo;
import com.niit.agricstore.entity.Item;
import com.niit.agricstore.service.ItemService;
import com.niit.agricstore.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/item")
public class ItemController {
    @Autowired
    ItemService itemService;
    //分页查询所有产品库存相关信息
    @GetMapping("/findItemAllByPage/{pageNum}/{pageSize}")
    public Result<PageInfo<Item>> findItemAllByPage(@PathVariable("pageNum")int pageNum, @PathVariable("pageSize")int pageSize){
        PageInfo<Item> list = itemService.findItemAllByPage(pageNum,pageSize);
        return Result.success(list);
    }


    //按照库存产品id查找
    @GetMapping("/selectByItemId/{itemId}")
    public Result<List<Item>> selectByItemId(@PathVariable Integer itemId ) {
        List<Item> itemList = itemService.selectByItemId(itemId);
        return Result.success(itemList);
    }
    //按照库存产品种类查找
    @GetMapping("/findAllByItemCategory/{itemCategory}")
    public Result<List<Item>> findAllByItemCategory(@PathVariable String itemCategory){
        List<Item> itemList = itemService.findAllByItemCategory(itemCategory);
        return Result.success(itemList);
    }
    //查询指定产品的库存与保质期
    @GetMapping("/findItemStockAndItemShelfLifeByItemId/{itemId}")
   public Result<List<Item>> findItemStockAndItemShelfLifeByItemId(@PathVariable Integer itemId){
        List<Item> itemList=itemService.findItemStockAndItemShelfLifeByItemId(itemId);
        return Result.success(itemList);
    }
    //查询指定产品单价
    @GetMapping("/findItemUnitPriceByItemId/{itemId}")
   public Result<List<Item>> findItemUnitPriceByItemId(@PathVariable Integer itemId){
        List<Item> itemList=itemService.findItemUnitPriceByItemId(itemId);
        return Result.success(itemList);
    }
    //库存表中插入商品各项数据
    @PostMapping("/insertAll")
    public Result insertAll( Item item) {
        int rows=itemService.insertAll(item);
        if (rows > 0){
            return Result.success(item);
        }else {
            return Result.error("200","没加上");
        }
    }
    //按照产品id删除产品
    @DeleteMapping("/delByItemId/{itemId}")
    public Result delByItemId(@PathVariable int itemId) {
        int rows=itemService.delByItemId(itemId);
        if (rows > 0){
            return Result.success("200","删没了~",rows);
        }else {
            return Result.error("202","没删掉");
        }
    }
    //更新指定产品的库存与生产日期
    @PutMapping("/updateItemStockAndItemProductionDateByItemId")
    public Result updateItemShelfLifeAndItemProductionDateByItemId(@RequestParam int itemStock, @RequestParam Date itemProductionDate, @RequestParam int itemId){
        int rows=itemService.updateItemStockAndItemProductionDateByItemId(itemStock,itemProductionDate,itemId);
        if (rows > 0){
            return Result.success("改好了");
        }else {
            return Result.error("200","没改好");
        }

    }
    //更新指定产品单价
    @PutMapping("updateItemUnitPriceByItemId")
    public Result updateItemUnitPriceByItemId(@RequestParam BigDecimal itemUnitPrice, @RequestParam int itemId){
       int rows=itemService.updateItemUnitPriceByItemId(itemUnitPrice,itemId);
        if (rows > 0){
            return Result.success("改好了");
        }else {
            return Result.error("200","没改好");
        }
    }




}
